package com.example.sx_06.suxuantv;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;

/**
 * @author liuyongkui
 *
 */
public class TcMouseView extends FrameLayout {

    private int mOffsetX;
    private int mOffsetY;
    public ImageView mMouseView;
    private Bitmap mMouseBitmap;
    private TcMouseManager mMouseManager ;
    private int mMouseX = TcMouseManager.MOUSE_STARTX;
    private int mMouseY = TcMouseManager.MOUSE_STARY;
    private int mLastMouseX = mMouseX;
    private int mLastMouseY = mMouseY;
    private int mMoveDis =  TcMouseManager.MOUSE_MOVE_STEP;
    private OnMouseListener mOnMouseListener;
    public TcMouseView(Context context) {
        super(context);
    }
    public TcMouseView(Context context, TcMouseManager mMouseMrg) {
        super(context);
        init( mMouseMrg);
    }

    public OnMouseListener getOnMouseListener() {
        return mOnMouseListener;
    }
    public void setOnMouseListener(OnMouseListener mOnMouseListener) {
        this.mOnMouseListener = mOnMouseListener;
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (mMouseView != null && mMouseBitmap != null) {
            mMouseView.measure(MeasureSpec.makeMeasureSpec(mMouseBitmap.getWidth(), MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(mMouseBitmap.getHeight(), MeasureSpec.EXACTLY));
        }
    }

    private void init(TcMouseManager manager) {
        mMouseManager  = manager;
        Drawable drawable =	getResources().getDrawable( R.mipmap.shubiao);
        mMouseBitmap = drawableToBitamp(drawable);
        mMouseView = new ImageView(getContext());
        mMouseView.setImageBitmap(mMouseBitmap);
        addView(mMouseView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        mOffsetX = (int)((mMouseBitmap.getWidth()));
        mOffsetY = (int)((mMouseBitmap.getHeight()));
        mOffsetX = (int)((mMouseBitmap.getWidth())*30/84);
        mOffsetY = (int)((mMouseBitmap.getHeight())*20/97);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,  int bottom) {
        if(mMouseView != null) {
            mMouseView.layout(mMouseX, mMouseY, mMouseX + mMouseView.getMeasuredWidth(), mMouseY + mMouseView.getMeasuredHeight());
        }
    }

    /**
     * 计算移动量
     * @param event
     */
    private void scrollView(KeyEvent event) {
        if(mMouseManager .getCurrentActivityType() == TcMouseManager.MOUSE_TYPE) {
            int pageScrollBy = 0;
            if(event.getKeyCode() == TcMouseManager.KEYCODE_UP) {
                pageScrollBy = - mMoveDis;
            } else if (event.getKeyCode() == TcMouseManager.KEYCODE_DOWN) {
                pageScrollBy = mMoveDis;
            }
            this.dispatchKeyEvent(event)	;
            /* MainActivity.contentView.dispatchKeyEvent(event);*/
        }
    }

    /**
     * 点击
     * @param event
     */
    public void onCenterButtonClicked(KeyEvent event) {
        LogUtils.d("点击X:"+mMouseX + mOffsetX +"Y:"+ mMouseY + mOffsetY);
        int x = mMouseX + mOffsetX;
        int y= mMouseY + mOffsetY;
        mMouseView.getX();
        mMouseManager .sendCenterClickEvent(mMouseX + mOffsetX, mMouseY + mOffsetY, event.getAction());//加一点偏移补�?
        setMouseClick(   mMouseView.getX(),   mMouseView.getY());
    }

    /**
     * 模拟点击
     * @param x
     * @param y
     */
    public  void  setMouseClick(float x,float y) {
        ToastUtils.showShort("X:"+x+"Y:"+y);
        MotionEvent evenDownt = MotionEvent.obtain(System.currentTimeMillis(),
                System.currentTimeMillis() + 100, MotionEvent.ACTION_DOWN, x-5, y-5, 0);
        dispatchTouchEvent(evenDownt);
        MotionEvent eventUp = MotionEvent.obtain(System.currentTimeMillis(),
                System.currentTimeMillis() + 100, MotionEvent.ACTION_UP, x-5, y-5, 0);
        dispatchTouchEvent(eventUp);
        evenDownt.recycle();
        eventUp.recycle();
    }

    private Bitmap drawableToBitamp(Drawable drawable) {
        BitmapDrawable bd = (BitmapDrawable) drawable;
        Bitmap bitmap = bd.getBitmap();
        return Bitmap.createScaledBitmap(bitmap, 50, 50 ,true);
    }
    /**
     * 移动
     * @param event
     * @param times
     */
    public void moveMouse(KeyEvent event, int times) {
        mMoveDis = times * TcMouseManager.MOUSE_MOVE_STEP;
        switch(event.getKeyCode()) {
            case TcMouseManager.KEYCODE_UP:
                if(mMouseY - mMoveDis >= 0) {
                    mMouseY = mMouseY - mMoveDis;
                } else {
                    mMouseY = 0;
                    scrollView(event);
                }
                break;
            case TcMouseManager.KEYCODE_LEFT:
                mMouseX = (mMouseX - mMoveDis > 0) ? mMouseX - mMoveDis : 0;
                break;
            case TcMouseManager.KEYCODE_DOWN:
                if(mMouseY + mMoveDis < getMeasuredHeight() - mMoveDis) {
                    mMouseY = mMouseY + mMoveDis;
                } else {
                    mMouseY = getMeasuredHeight() - mOffsetY;
                    scrollView(event);
                }
                break;
            case TcMouseManager.KEYCODE_RIGHT:
                mMouseX = (mMouseX + mMoveDis < getMeasuredWidth() - mOffsetX) ? mMouseX + mMoveDis : getMeasuredWidth() - mOffsetX;
                break;
        }
        if(mLastMouseX == mMouseX && mLastMouseY == mMouseY) {
            return;
        }
        mLastMouseX = mMouseX;
        mLastMouseY = mMouseY;
        requestLayout();
        mMouseManager .sendMouseHoverEvent(mMouseX + mOffsetX, mMouseY + mOffsetY);
    }

    /**
     * 点击接口
     * @author liuyongkui
     *
     */
    public interface OnMouseListener {
        boolean onclick(View v, KeyEvent event);
    }

}